package com.example.elmmerchants.Message_Setting;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Computer_Auto_Reply extends AppCompatActivity {

    Switch is_open_computer_reply_switch, is_open_supplementary_delivery_auto_reply_switch, is_open_auto_reminder_order_reply_switch;

    String email;
    int is_open_computer_reply, is_open_supplementary_delivery_auto_reply, is_open_auto_reminder_order_reply;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_computer_auto_reply);

        email = getIntent().getStringExtra("email");
        is_open_computer_reply = getIntent().getIntExtra("is_open_computer_reply", 0);
        is_open_supplementary_delivery_auto_reply = getIntent().getIntExtra("is_open_supplementary_delivery_auto_reply", 0);
        is_open_auto_reminder_order_reply = getIntent().getIntExtra("is_open_auto_reminder_order_reply", 0);

        is_open_computer_reply_switch = findViewById(R.id.is_open_computer_reply_switch);
        is_open_supplementary_delivery_auto_reply_switch = findViewById(R.id.is_open_supplementary_delivery_auto_reply_switch);
        is_open_auto_reminder_order_reply_switch = findViewById(R.id.is_open_auto_reminder_order_reply_switch);

        if (is_open_computer_reply == 1) {
            is_open_computer_reply_switch.setChecked(true);
        }
        else {
            is_open_computer_reply_switch.setChecked(false);
        }

        if (is_open_supplementary_delivery_auto_reply == 1) {
            is_open_supplementary_delivery_auto_reply_switch.setChecked(true);
        }
        else {
            is_open_supplementary_delivery_auto_reply_switch.setChecked(false);
        }

        if (is_open_auto_reminder_order_reply == 1) {
            is_open_auto_reminder_order_reply_switch.setChecked(true);
        }
        else {
            is_open_auto_reminder_order_reply_switch.setChecked(false);
        }

        is_open_computer_reply_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_computer_reply_switch.isChecked()) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_computer_reply = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_computer_reply_switch.setChecked(true);
                            is_open_computer_reply = 1;
                        });
                    });
                }
                else {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_computer_reply = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 0);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_computer_reply_switch.setChecked(false);
                            is_open_computer_reply = 0;
                        });
                    });
                }
            }
        });

        is_open_supplementary_delivery_auto_reply_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_supplementary_delivery_auto_reply_switch.isChecked()) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_supplementary_delivery_auto_reply = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_supplementary_delivery_auto_reply_switch.setChecked(true);
                            is_open_supplementary_delivery_auto_reply = 1;
                        });
                    });
                }
                else {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_supplementary_delivery_auto_reply = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 0);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_supplementary_delivery_auto_reply_switch.setChecked(false);
                            is_open_supplementary_delivery_auto_reply = 0;
                        });
                    });
                }
            }
        });

        is_open_auto_reminder_order_reply_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_auto_reminder_order_reply_switch.isChecked()) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_auto_reminder_order_reply = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_auto_reminder_order_reply_switch.setChecked(true);
                            is_open_auto_reminder_order_reply = 1;
                        });
                    });
                }
                else {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_auto_reminder_order_reply = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 0);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_auto_reminder_order_reply_switch.setChecked(false);
                            is_open_auto_reminder_order_reply = 0;
                        });
                    });
                }
            }
        });
    }
}